package com.concurrency.juc.synctools.lock.reentrant;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo1 {
    public static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            new Thread(new ThreadTest(i)).start();
        }
    }

    static class ThreadTest implements Runnable {
        Integer id;

        public ThreadTest(Integer id) {
            this.id = id;
        }

        public void run() {
            try {
                TimeUnit.MILLISECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 2; i++) {
                lock.lock();
                System.out.println("获取锁的线程：" + id);
                lock.unlock();
            }
        }
    }


}
